<?php
/**
* @package      auto_occasion
* @subpackage   entreprise
* @author       CMS
* @copyright    2011
* @license      All rights reserved
*/


/**
 * Classe permettant de gérer les entreprises
 * ajout , rechercher
 *
 */
class BoEntrepriseCtrl extends jController {
    
public $pluginParams = array(
                                    '*'=>array('auth.required'=>true,
                                    'jacl2.right'=>'admin.compte.gerer'
                                    )
                            );

    /**
     * Liste des entreprises
     *
     *
     */
    function index() {

        jClasses::inc("entreprise~entrepriseSrv");
        jClasses::inc("entreprise~typeSrv");
        jClasses::inc("annonce~annonceCoordonneesSrv");
        jClasses::inc("commun~common");


        $offset        =  $this->intParam('offset',0,true);
        $nbitemparpage = ENTREPRISE_NBITEMPARPAGE;
        $properties = array('start-label' => '',
                              'prev-label'  => '<',
                              'next-label'  => '>',
                              'end-label'   => '',
                              'area-size'   => 1);
        $id                  = $this->intParam('id', '');
        $idType              = $this->intParam('idType', 0);

        $type           = typeSrv::liste();
        $departement    = annonceCoordonneesSrv::listeDepartement();
        $entrepriseList = entrepriseSrv::listeEntrepriseDepartements($idType);
		$entreprise     = ($id)? entrepriseSrv::chargeParId($id) : entrepriseSrv::init();
        $enAvant        = ($id)? entrepriseSrv::getEnterpriseEnAvant($id) : array();
		$typeEntreprise = ($idType)? entrepriseSrv::typeEntrepriseParIdType($idType) : common::init('ao_type_entreprise');

		$departementParTypeEntreprise = array();
		foreach($entrepriseList as $item){
			if(!in_array($item->departement_num .' '. $item->departement_nom, $departementParTypeEntreprise)){
				array_push($departementParTypeEntreprise,$item->departement_num .' '. $item->departement_nom);
			}
		}
		

        $rep = $this->getResponse('html');
   		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.validation.js');

        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'ckeditor/ckeditor.js');
        $rep->menusActifs = adminHtmlResponse::MENU_ENTREPRISE;

        $rep->body->assign('nbitemparpage', $nbitemparpage);
        $rep->body->assign('page', $offset);
        $rep->body->assign('properties',       $properties);
        $rep->body->assign('type',             $type);
		$rep->body->assign('typeEntreprise',   $typeEntreprise);
        $rep->body->assign('departement',      $departement);
        $rep->body->assign('id',               $id);
        $rep->body->assign('idType',           $idType);
        $rep->body->assign('entreprise',       $entreprise );
        $rep->body->assign('entrepriseByType', $entrepriseList);
		$rep->body->assign('departementParTypeEntreprise', array_unique($departementParTypeEntreprise));
        $rep->body->assign('enAvant', $enAvant);


        $rep->bodyTpl = 'entreprise~BoEntrepriseEdit';
        return $rep;
    }


    /**
     * Mise en avant des entreprises par département
     */
    function miseEnAvant() {

        jClasses::inc("annonce~annonceCoordonneesSrv");
        jClasses::inc("commun~common");


        $departement    = annonceCoordonneesSrv::listeDepartement();
        $type           = common::liste('ao_type_entreprise');

        
        $rep = $this->getResponse('html');
        $rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/jquery-ui.css');

		$rep->addCSSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/css/ui.multiselect.css');

        $rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery-ui.min.js');
		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/jquery.localisation-min.js');
		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/ui.multiselect.js');
   		$rep->addJSLink($GLOBALS['gJConfig']->urlengine['basePath'].'design/back/js/entreprise.js');


        $rep->menusActifs = adminHtmlResponse::MENU_ENTREPRISE;

        $rep->body->assign('departement',      $departement);
        $rep->body->assign('type',      $type);


        $rep->bodyTpl = 'entreprise~BoEntrepriseMiseEnAvant';
        $rep->body->assignZone ('regionListe', 'entreprise~regionListe', array()) ;
        $rep->body->assignZone ('entrepriseListe', 'entreprise~entrepriseListe', array()) ;
        $rep->body->assignZone ('departementListe', 'entreprise~departementListe', array()) ;

        return $rep;
    }


   /**
     * sauvegarder les mises en avant pour les entreprises
     *
     */
     function saveEnAvant() {

        jClasses::inc("entreprise~entrepriseSrv");
        
        $departement = $this->intParam('departement', 0);
        $entreprise  = $this->param('entreprise', 0);
        $type        = $this->intParam('type', 0);
        $region      = $this->stringParam('region', '');
        if ($type && $region) {
            // Vider les enregistrement dans ao_misenavant_dep_entreprise correspondant au departement/region/type en cours
            entrepriseSrv::delByDept($departement, $type, $region);

            //Inserer les nouvelles données
            entrepriseSrv::addEnAvantByDept($departement, $entreprise, $type, $region);
        }
        $rep = $this->getResponse('redirect');
        $rep->action = 'entreprise~BoType:index';

        return $rep;
    }


   /**
     * sauvegarder une entreprise
     *
     */
     function sauvegarder() {

        jClasses::inc("entreprise~entrepriseSrv");
        jClasses::inc("commun~communSrv");

        $entreprise                            = new stdClass();
        $entreprise->entreprise_piece          = $this->intParam('entreprise_piece', 0);
        $entreprise->entreprise_ouverture      = $this->stringParam('entreprise_ouverture', '');
        $entreprise->entreprise_name           = $this->stringParam('societe','');
        $entreprise->entreprise_adresse        = $this->stringParam('adresse','');
        $entreprise->entreprise_cp             = $this->stringParam('cp','');
        $entreprise->entreprise_ville          = $this->stringParam('ville','');
        $entreprise->entreprise_tel            = $this->stringParam('tel','');
        $entreprise->entreprise_tel2           = $this->stringParam('tel2','');
        $entreprise->entreprise_fax            = $this->stringParam('fax','');
        $entreprise->entreprise_email          = $this->stringParam('email','');
        $entreprise->entreprise_site           = $this->stringParam('site','');
        $entreprise->entreprise_departement    = $this->stringParam('departement','');
        $entreprise->entreprise_description    = $this->stringParam('content','');
        $entreprise->entreprise_type_id        = $this->param('type','');
        $entreprise->miseenavant               = $this->param('miseenavant','');
        $entreprise->entreprise_logo           = communSrv::uploadImage($_FILES['logo'], ENTREPRISE_PATH_PHOTO);
        $entreprise->ordre                     = entrepriseSrv::updateOrdre($entreprise->miseenavant);
        $entreprise->entreprise_id             = $this->intParam('id','');
		$type = (int)$entreprise->entreprise_type_id;

        if ($entreprise->entreprise_id){
             $entreprise = entrepriseSrv::modifier($entreprise);
        } else {
             $entreprise = entrepriseSrv::ajouter($entreprise);
        }
		
        $rep = $this->getResponse('redirect');
        $rep->action = 'entreprise~BoEntreprise:index';
		$rep->params = array('idType'=>$type);
        return $rep;
    }

   }

